# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

default_platform(:ios)

platform :ios do
  desc "Clean environment"
  lane :clean do
    sh <<~EOS
      cd ../..
      .flutter/bin/flutter clean
      .flutter precache --ios
      .flutter/bin/flutter pub get
    EOS
  end

  desc "Run Flutter unit tests"
  lane :test do
    sh "../../.flutter/bin/flutter test"
  end

  desc "Build Flutter app for a specified flavor (default: f_generic)"
  lane :build do |options|
    flavor = options[:flavor] || "f_generic"
    sh <<~EOS
      HOMEBREW_NO_AUTO_UPDATE=1 # disable homebrew's automatic updates.
      brew install cocoapods
      cd ../../ios
      bundle check || bundle install
      bundle exec pod install --no-repo-update
    EOS
  end

  desc "Push a new release build to the App Store"
  lane :deploy do |options|
    flavor = options[:flavor] || "f_store"
    do_build = options.fetch(:build, true)
    do_clean = options.fetch(:clean, true)
    ipa_path = "../build/ios/Build/Fastlane/Release-#{flavor}"

    clean() if do_clean
    build(flavor: flavor) if do_build
    build_ios_app(
      workspace: "Runner.xcworkspace",
      scheme: flavor,
      output_directory: ipa_path
    )
    upload_to_testflight(
      ipa: "#{ipa_path}/Runner.ipa",
      uses_non_exempt_encryption: true
    )
  end
end
